package com.yq.ylmall.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.ylmall.acl.service.RoleService;
import com.yq.ylmall.acl.service.UserService;
import com.yq.ylmall.common.utils.MD5;
import com.yq.ylmall.dto.acl.AssignRoleDto;
import com.yq.ylmall.dto.acl.UserQueryDto;
import com.yq.ylmall.model.acl.User;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户管理 前端控制器
 */
@RestController
@RequestMapping("/admin/system/user")
@Api(tags = "用户管理接口")
public class UserController{
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleDto assignRoleDto) {
        userService.doAssign(assignRoleDto) ;
        return Result.build(null , ResultCode.SUCCESS) ;
    }

    @ApiOperation(value = "获取用户分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
            UserQueryDto userQueryVo) {
        Page<User> pageParam = new Page<>(page, limit);
        IPage<User> pageModel = userService.selectPage(pageParam, userQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("save")
    public Result save(@RequestBody User user) {
        userService.saveUser(user);
        return Result.ok();
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("update")
    public Result updateById(@RequestBody User user) {
        userService.updateUserById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        userService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "根据id列表删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        userService.removeByIds(idList);
        return Result.ok();
    }
}
